﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.Departments;

namespace MyMES.Writer.API.Application.CommendHandlers.Departments
{
    public class CreateDepartmentsCommendHandlers : IRequestHandler<CreateDepartmentsCommends, APIResult<Department>>
    {
        private readonly IBaseRepository<Department> departmentRepository;
        private readonly IMapper mapper;

        public CreateDepartmentsCommendHandlers(IBaseRepository<Department> departmentRepository, IMapper mapper)
        {
            this.departmentRepository = departmentRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 创建部门
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<Department>> Handle(CreateDepartmentsCommends request, CancellationToken cancellationToken)
        {
            APIResult<Department> result = new APIResult<Department>();
            result.Code = APIEnum.添加成功;
            result.Msg= "添加成功";
            var department = mapper.Map<Department>(request);
            var sql = "INSERT INTO department VALUES(@DepartmentID,@DepartmentNo,@DepartmentName)";
            await departmentRepository.Insert(department, sql);
            return result;
        }
    }
}
